package com.rock.minibatis.binding;

import com.rock.minibatis.config.Configuration;
import com.rock.minibatis.mapping.MappedStatement;
import com.rock.minibatis.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MapperProxy implements InvocationHandler {

    private final SqlSession sqlSession;
    private final Configuration configuration;

     public MapperProxy(SqlSession sqlSession, Configuration configuration){
        this.sqlSession = sqlSession;
        this.configuration = configuration;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String id = method.getDeclaringClass().getName()+"."+method.getName();
        MappedStatement mapped = configuration.getMappedStatement(id);
        if(mapped==null){
            System.out.println("您定义dao或方法在mapper中不存在");
            return null;
        }else{
            return sqlSession.selectList(id,args[0]);
        }
    }
}
